package com.example.mytips.structure;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBList;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBScrollPane;
import groovyjarjarantlr4.v4.runtime.misc.Nullable;
import org.apache.http.util.TextUtils;
import org.jetbrains.annotations.NonNls;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FlutterStructureDialog extends DialogWrapper {

    List<ClassEntity> classList;

    JBList<StructureDialogEntity> structureList;

    private AnActionEvent e;

    private JBPanel panel;

    private Editor editor;

    private JBScrollPane scrollPane;

    private int width = 850;
    private int height = 2024;

    private int methodNameWidth = 220;
    private int returnValueWidth = 160;
    private int noteWidth = width - methodNameWidth - returnValueWidth - 60;

    public FlutterStructureDialog(AnActionEvent e, List<ClassEntity> classList, String fileName,Editor editor,int cursorLine) {
        super(true);
        this.classList = classList;
        this.e = e;
        this.editor = editor;

        StructureDialogEntity lastMethodEntity = null;
        boolean isExe = false;
        List<StructureDialogEntity> rowList = new ArrayList<>();
        for (int i = 0; i < classList.size(); i++) {
            ClassEntity classEntity = classList.get(i);
            StructureDialogEntity groupEntity = new StructureDialogEntity();
            groupEntity.name = classEntity.name;
            groupEntity.isClass = true;
            groupEntity.line = classEntity.startLineNum;
            groupEntity.type = classEntity.type;
            rowList.add(groupEntity);

            for (int j = 0; j < classEntity.methodList.size(); j++) {
                MethodEntity methodEntity = classEntity.methodList.get(j);

                StructureDialogEntity entity = new StructureDialogEntity();
                entity.name = methodEntity.name;
                entity.note = methodEntity.note;
                entity.line = methodEntity.startLineNum;
                entity.returnValue = methodEntity.resultValue;
                entity.initSelected = false;

                if (cursorLine - entity.line >= 0 && lastMethodEntity != null) {
                    lastMethodEntity.initSelected = false;
                    entity.initSelected = true;
                } else if (cursorLine - entity.line >= 0 && lastMethodEntity == null) {
                    entity.initSelected = true;
                }

                rowList.add(entity);
                lastMethodEntity = entity;
            }
        }
        structureList = new JBList<>(rowList);
        structureList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        ///点击事件
        structureList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    // 获取子目录名称
                    StructureDialogEntity entity = structureList.getSelectedValue();
                    System.out.println("行数 " + entity.line +  " ,选中了 " + entity.name);


                    ///将光标定位到指定行
                    editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(entity.line, 0));
                    editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
                    doOKAction();

                }
            }
        });

        ///布局
        structureList.setCellRenderer(new ListCellRenderer<StructureDialogEntity>() {
            @Override
            public Component getListCellRendererComponent(JList<? extends StructureDialogEntity> list, StructureDialogEntity value, int index,
                                                          boolean isSelected, boolean cellHasFocus) {
                // 创建水平布局的面板
                JPanel panel = new JPanel();
                panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

                if (value.isClass) {
                    ///这是类型
                    String typeStr = value.type;
                    JLabel note = new JLabel(typeStr);
                    note.setForeground(new Color(0,255,0,150));
                    panel.add(note);

                    ///这是类
                    JLabel name = new JLabel(value.name);
                    name.setPreferredSize(new Dimension(name.getWidth(), 40)); // 设置JLabel的宽度和高度
                    name.setForeground(Color.RED);
                    name.setFont(new Font("Arial",Font.BOLD,20));
                    panel.add(name);

                } else {

                    ///这是方法名
                    JLabel name = new JLabel(value.name);

                    ///设置最大宽高
                    name.setMaximumSize(new Dimension(methodNameWidth, 30));
                    name.setMinimumSize(new Dimension(methodNameWidth, 30));

                    ///设置宽高
                    Dimension preferredSize = new Dimension(methodNameWidth, 30);
                    name.setPreferredSize(preferredSize);

                    ///文本颜色
                    //name.setForeground(Color.WHITE);
                    ///字体
                    name.setFont(new Font("Arial",Font.PLAIN,14));

                    ///背景颜色
                    if (index % 2 == 0) {
                        panel.setBackground(new Color(54,54,54));
                    } else {
                        panel.setBackground(new Color(28,28,28));
                    }

                    ///选中的
                    if (value.initSelected) {
                        panel.setBackground(new Color(139,101,8));
                    }

                    panel.add(name);

                    ///这是返回值
                    JLabel returnValueLabel = new JLabel(value.returnValue);
                    Dimension dimension = new Dimension(returnValueWidth, returnValueLabel.getPreferredSize().height);
                    returnValueLabel.setMaximumSize(dimension);
                    returnValueLabel.setMinimumSize(dimension);
                    returnValueLabel.setPreferredSize(dimension);
                    panel.add(returnValueLabel);


                    ///注释
                    if (!TextUtils.isEmpty(value.note)) {
                        String noteStr = value.note;
                        //noteStr = "在上述代码中，我们创建了一个JFrame和一个JPanel。在JPanel中，我们使用FlowLayout布局管理器，它会根据组件的大小和位置自动布局。然后，我们创建一个JLabel，并使用setFont()方法设置其字体，通过调整字体的大小和样式来间接控制行高。接下来，我们使用setBorder()方法设置一个具有10像素上下内边距的边框，以增加行高。最后，我们将JLabel添加到JPanel中，并将JPanel添加到JFrame中。调用pack()方法可以根据组件的大小自动调整JFrame的大小";
                        JLabel note = new JLabel(noteStr);
                        note.setForeground(new Color(0,255,0,150));
                        panel.add(note);

                    }
                }

                return panel;
            }
        });


        // 创建面板
        panel = new JBPanel(new BorderLayout());
        panel.add(structureList,BorderLayout.CENTER);

        // 将JBPanel放置在JScrollPane中
        scrollPane = new JBScrollPane(panel);
        getContentPane().add(scrollPane);

        // 设置对话框界面布局
        setTitle(fileName);
        // 固定窗口大小
        setResizable(false);
        // 窗口大小
        setSize(width,height);
        pack();
        // 初始化对话框内容
        init();

    }

    @Override
    protected @NonNls @org.jetbrains.annotations.Nullable String getDimensionServiceKey() {
        return "FlutterStructureDialog";
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        return scrollPane;
    }

}

class StructureDialogEntity {
    boolean isClass;
    int line;
    Boolean isMark;
    String type;
    String markNote;
    boolean initSelected;
    String name;
    String note;
    String returnValue;
}